package cn.edu.scau.cmi.front.service;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.scau.cmi.ema.domain.Address;
import cn.edu.scau.cmi.ema.domain.Event;
import cn.edu.scau.cmi.ema.domain.Foodtype;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;
import cn.edu.scau.cmi.yanghongyu.dao.FrontEchartDAO;

//杨宏宇同学编写
@Service
@Transactional
public class FrontEchartServiceImpl extends CmiEmaService implements FrontEchartService{
	
	@Autowired
	FrontEchartDAO frontEchartDAO;
	
	@Override
	public void fillupEchart() {
		int startResult = 0;
		int maxRows = 300;
		
		while (true) {
			Set<Event> events = eventDAO.findAllEvents(startResult, maxRows);
			if (events.isEmpty()) break;
			
			for (Event event : events) {
				Integer eventId = event.getId();
				String eventName = event.getName();
				String foodtype = event.getFoodbatch().getFood().getFoodtype().getName();
				
				String sourceCountry = null;
				String sourceProvince = null;
				String sourceCity = null;
				String discoveryCountry = null;
				String discoveryProvince = null;
				String discoveryCity = null;
				
				Address address = event.getSourceAddress();
				while (address != null && address.getLevel() > 1) {
					if (address.getLevel() == 4) sourceCity = address.getName();
					else if (address.getLevel() == 3) sourceProvince = address.getName();
					else if (address.getLevel() == 2) sourceCountry = address.getName();
					address = address.getParent();
				}
				
				address = event.getDiscoveryAddress();
				while (address != null && address.getLevel() > 1) {
					if (address.getLevel() == 4) discoveryCity = address.getName();
					else if (address.getLevel() == 3) discoveryProvince = address.getName();
					else if (address.getLevel() == 2) discoveryCountry = address.getName();
					address = address.getParent();
				}
				
				System.out.println("事件id : " + eventId);
				
				frontEchartDAO.updateOrSave2echartBySql(eventId, eventName, sourceCountry, sourceProvince, sourceCity, 
						discoveryCountry, discoveryProvince, discoveryCity, foodtype);
			}
			
			startResult += events.size();
		}
		
		System.out.println("导入成功");
	}

	@Override
	public void fillupFoodtypeIdOfEchart() {
		int startResult = 0;
		int maxRows = 300;
		Set<Integer> foodtypeSet = new HashSet<>();
		foodtypeSet.add(1642);
		foodtypeSet.add(565);
		foodtypeSet.add(583);
		foodtypeSet.add(566);
		foodtypeSet.add(562);
		foodtypeSet.add(591);
		
		while (true) {
			Set<Event> events = eventDAO.findAllEvents(startResult, maxRows);
			if (events.isEmpty()) break;
			
			for (Event event : events) {
				Integer eventId = event.getId();
				Foodtype foodtype = event.getFoodbatch().getFood().getFoodtype();
				Integer foodtypeId = null;
				
				while (foodtype.getId() > 561) {
					if (foodtypeSet.contains(foodtype.getId())) {
						foodtypeId = foodtype.getId();
						break;
					}
					foodtype = foodtype.getParent();
				}
				
				if (foodtypeId == null) continue;
				
				System.out.println(foodtypeId);
				frontEchartDAO.updateFoodtypeIdBySql(eventId, foodtypeId);
			}
			
			startResult += events.size();
		}
		
		System.out.println("导入成功");
	}
}